package ro.gateway.aida.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Organization;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.srv.EditorServlet;
import ro.gateway.aida.srv.IIDGenerator;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.HttpUtils;

/**
 * @deprecated
 * @author Mihai Postelnicu<p>
 *
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 */
public class EditOrganizationServlet
	  extends EditorServlet
{
	  private String ctx;

	  public EditOrganizationServlet()
	  {
	  }

	  protected void edit( HttpServletRequest request, HttpServletResponse response ) throws
			javax.servlet.ServletException, java.io.IOException
	  {
			request.setAttribute( PNAME_SCREEN, PV_SCR_EDIT );

			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			Organization item = ( Organization ) bean.get( PNAME_ORG );

			String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );

			String back = HttpUtils.getValidTrimedString( request, "back", null );
			if ( back == null )
			{

				  item.setAcronym( HttpUtils.getValidTrimedString( request, "acronym", "" ) );
				  item.setCatID( HttpUtils.getInt( request, "catid", -1 ) );
				  item.setCountryISO3( HttpUtils.getValidTrimedString( request, "countryiso3", null ) );
				  item.getDescription().setString( lang,
						HttpUtils.getValidTrimedString( request, "description", null ) );
				  item.getName().setString( lang, HttpUtils.getValidTrimedString( request, "name", null ) );
				  item.setAIDAOrg( HttpUtils.getValidTrimedString( request, "aidaorg", "" ) );
				  bean.put( PNAME_ORG, item );
			}
			request.getRequestDispatcher( JSP_PAGE ).
				  forward( request, response );
	  }

	  protected void other_action_performed( HttpServletRequest request,
											 HttpServletResponse response,
											 String action ) throws javax.servlet.
			ServletException, java.io.IOException
	  {
	  }

	  protected void confirm( HttpServletRequest request,
							  HttpServletResponse response ) throws javax.servlet.
			ServletException, java.io.IOException
	  {

			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			String key = ( String ) bean.get( EditOrganizationServlet.PNAME_KEY );
			Organization item = ( Organization ) bean.get( PNAME_ORG );
			String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );
			User user = UserUtils.getLogged( request, response );
			if ( user == null ) return;


			//form the Organization bean
			if ( item.getUserID() == -1 ) item.setUserID( user.getId() );
			item.setAcronym( HttpUtils.getValidTrimedString( request, "acronym", null ) );
			item.setCatID( HttpUtils.getInt( request, "catid", -1 ) );
			item.setCountryISO3( HttpUtils.getValidTrimedString( request, "countryiso3", null ) );
			item.getDescription().setString( lang,
				  HttpUtils.getValidTrimedString( request, "description", null ) );
			item.getName().setString( lang, HttpUtils.getValidTrimedString( request, "name", null ) );
			item.setAIDAOrg( HttpUtils.getValidTrimedString( request, "aidaorg", "" ) );


			Vector persel = null;
			Hashtable beans = null;
			Hashtable persels = null;
			beans = ( Hashtable ) request.getSession().getAttribute( PersSelectorServlet.
				  PNAME_BEANS );
			if ( beans != null )
			{
				  persels = ( Hashtable ) beans.get( PersSelectorServlet.PNAME_PERSELS );
			}
			if ( persels != null )
			{
				  persel = ( Vector ) persels.get( "org_pers" + key );
			}
			if ( persel != null )
			{
				  item.setPersons( persel );

			}
			request.setAttribute( PNAME_SCREEN, PV_SCR_CONFIRM );
			request.getRequestDispatcher( JSP_PAGE ).
				  forward( request, response );
	  }

	  protected void start_editing(
			ServletContext application,
			HttpServletRequest request,
			HttpServletResponse response ) throws IOException, ServletException
	  {
			super.start_editing( application, request, response );
			response.sendRedirect(request.getContextPath()+"/org/editorg.do");
			
/*
			HttpSession session = request.getSession();
			Hashtable beans = ( Hashtable ) session.getAttribute( PNAME_BEANS );
			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			PersistenceToken token = PersistenceManager.tokenLookup( application );
			String action = ( String ) bean.get( PNAME_ACTION );
			String key = ( String ) request.getAttribute( PNAME_KEY );

			if ( PV_ACTION_EDIT.equals( action ) || PV_ACTION_DELETE.equals( action ) )
			{

				  long orgid = HttpUtils.getLong( request, "orgid", -1 );
				  String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );

				  Organization org = null;
				  try
				  {
						OrganizationDB orgdb = OrganizationDB.getManager(token);
						org = orgdb.getByID( orgid );

						//revive persons selected
						Hashtable beans2 = ( Hashtable ) request.getSession().getAttribute(
							  PersSelectorServlet.
							  PNAME_BEANS );
						Hashtable persels = null;
						Vector persel = null;
						if ( beans2 == null )
						{
							  beans2 = new Hashtable();
							  request.getSession().setAttribute( PersSelectorServlet.PNAME_BEANS,
									beans2 );
						}
						persels = ( Hashtable ) beans2.get( PersSelectorServlet.PNAME_PERSELS );


						if ( persels == null )
						{
							  persels = new Hashtable();
							  beans2.put( PersSelectorServlet.PNAME_PERSELS, persels );
						}
						if ( org.getPersons() != null )
							  persels.put( "org_pers" + key, org.getPersons() );
						else
							  persels.put( "org_pers" + key, new Vector() );

				  }
				  catch ( SQLException e )
				  {
						e.printStackTrace();
						request.setAttribute( PNAME_SCREEN, PV_SCR_ERROR );
						request.setAttribute( PNAME_ERRORS, "User not found" );
						request.getRequestDispatcher( JSP_PAGE ).
							  forward( request, response );
						return;
				  }
				  if ( org == null )
				  {
						org = new Organization();
						org.setId( orgid );
				  }
				  bean.put( PNAME_ORG, org );
				  bean.put( PNAME_ACTION, action );
			}
			else if ( PV_ACTION_NEW.equals( action ) )
			{
				  Organization org = new Organization();
				  bean.put( PNAME_ORG, org );
			}
			else
			{
				  beans.remove( key );
				  response.sendError( HttpServletResponse.SC_NOT_FOUND );
				  return;
			}
*/
	  	  }

	  public String getPNAME_BEANS()
	  {
			return PNAME_BEANS;
	  }

	  protected void cancel(
			HttpServletRequest request,
			HttpServletResponse response ) throws IOException, ServletException
	  {
			super.cancel( request, response );
	  }

	  protected void save( ServletContext application, HttpServletRequest request,
						   HttpServletResponse response ) throws javax.servlet.
			ServletException, java.io.IOException
	  {
			Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
			Hashtable beans = ( Hashtable ) request.getSession().getAttribute( PNAME_BEANS );
			Organization org = ( Organization ) bean.get( PNAME_ORG );
			PersistenceToken token = PersistenceManager.tokenLookup( application );
			String action = ( String ) bean.get( PNAME_ACTION );
			String[] errors = null;
			String lang = HttpUtils.getValidTrimedString( request, "lang", "ro" );
			User user = UserUtils.getLogged( request, response );
			if ( user == null ) return;
//			if ( !UserUtils.checkRights( user, request, response, org.getUserID() ) ) return;

			IIDGenerator idGenerators = ( IIDGenerator ) ( ( Hashtable ) application.
				  getAttribute( Constants.SERVICES ) ).
				  get( Constants.SERVICE_IDGEN );

			try
			{
				  OrganizationDB orgdb = OrganizationDB.getManager(token);
				  if ( PV_ACTION_DELETE.equals( action ) )
				  {
						ctx=request.getContextPath();
						orgdb.delete( org.getId() );
						response.sendRedirect(ctx+"/vieworg.do");
						return;
				  }
				  else if ( PV_ACTION_NEW.equals( action ) )
				  {
						orgdb.insert(  org, lang );
				  }
				  else
				  {
						orgdb.update( org, lang );
				  }
			}
			catch ( SQLException e )
			{
				  errors = new String[]{
						e.getMessage()};
			}

			if ( errors == null )
			{
				  beans.remove( bean.get( PNAME_KEY ) );
				  response.sendRedirect( ( String ) bean.get( PNAME_REFERER ) );
				  return;
			}
			else
			{
				  request.setAttribute( PNAME_SCREEN, PV_SCR_ERROR );
				  request.setAttribute( PNAME_ERRORS, errors );
				  request.getRequestDispatcher( JSP_PAGE ).
						forward( request, response );
				  return;
			}

	  }

	  public String getJSP_PAGE()
	  {
			return JSP_PAGE;
	  }

	  public static final String PNAME_BEANS = "EDITING_ORGANIZATIONS";
	  public static final String PNAME_ORG = "ORG";
	  public static final String JSP_PAGE = "organizatii.jsp";
	  public static final String PNAME_ERRORS = "errors";

}
